//
//  PowerAllocator.h
//  MemoryManager
//
//  Created by Bram Buurlage on 6/28/12.
//  Copyright (c) 2012 Bram Buurlage. All rights reserved.
//

#ifndef MemoryManager_PowerAllocator_h
#define MemoryManager_PowerAllocator_h

#include "Allocator.h"
#include "PowerOfTwoArea.h"

class BuddyListHeader;
class BuddyAllocator: public Allocator
{
public:
    BuddyAllocator();
    virtual ~BuddyAllocator();
    
    virtual void        printFragmentation();
    
    virtual const Area* alloc(int wanted);
    virtual void        free(const Area*);
    
    virtual void        setSize(int size);
    
private:
    double      baselog;
    int         minpower;
    int         maxpower;
    BuddyListHeader** headers;
};

class BuddyListHeader
{
public:
    BuddyListHeader(int size);
    ~BuddyListHeader();
    
    void printFragmentation();
    
    void setHighLevel(BuddyListHeader* level);
    void addNewArea(PowerOfTwoArea* a);
    Area* alloc();
    void free(PowerOfTwoArea* a);
    
private:
    int size;
    BuddyListHeader*     higherLevel;
    PowerOfTwoArea* firstFreeArea;
};

#endif
